// fast4j - Fast & Agile Service Tools for Java
// Copyright (C) 2007 Alexandre ROMAN
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

// $Id: ServiceCall.java 12 2007-03-25 22:13:16Z alexandre.roman $

package fast4j.ejbbridge;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import fast4j.ejbbridge.util.MethodUtils;

/**
 * Call to service method.
 * @author alexandre.roman
 */
public class ServiceCall implements Externalizable {
    private static int hashCode(Object[] array) {
        final int prime = 31;
        if (array == null)
            return 0;
        int result = 1;
        for (int index = 0; index < array.length; index++) {
            result = prime * result
                    + (array[index] == null ? 0 : array[index].hashCode());
        }
        return result;
    }
    
    private static final long serialVersionUID = 1L;
    private String className;
    private int methodHash;
    private Object[] arguments;
    private Map context;
    
    /**
     * This constructor is required for proper serialization. Don't use it!
     */
    public ServiceCall() {
    }
    
    /**
     * Constructs a new instance.
     * @param className service interface (required)
     * @param methodHash method identifier, generated by
     *            {@link MethodUtils#hash(java.lang.reflect.Method)}
     * @param arguments method arguments
     */
    public ServiceCall(final String className, final int methodHash,
            final Object[] arguments, final Map context) {
        if (className == null) {
            throw new IllegalArgumentException(
                    "className is required and cannot be null");
        }
        
        this.className = className;
        this.methodHash = methodHash;
        this.arguments = arguments;
        this.context = context == null ? new HashMap() : context;
    }
    
    /**
     * Constructs a new instance, without context.
     * @see #ServiceCall(String, int, Object[], Map)
     */
    public ServiceCall(final String className, final int methodHash,
            final Object[] arguments) {
        this(className, methodHash, arguments, null);
    }
    
    public Object[] getArguments() {
        return arguments;
    }
    
    public String getClassName() {
        return className;
    }
    
    public int getMethodHash() {
        return methodHash;
    }
    
    public Map getContext() {
        return context;
    }
    
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ServiceCall.hashCode(arguments);
        result = prime * result
                + ((className == null) ? 0 : className.hashCode());
        result = prime * result + ((context == null) ? 0 : context.hashCode());
        result = prime * result + methodHash;
        return result;
    }
    
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final ServiceCall other = (ServiceCall) obj;
        if (!Arrays.equals(arguments, other.arguments))
            return false;
        if (className == null) {
            if (other.className != null)
                return false;
        } else if (!className.equals(other.className))
            return false;
        if (context == null) {
            if (other.context != null)
                return false;
        } else if (!context.equals(other.context))
            return false;
        if (methodHash != other.methodHash)
            return false;
        return true;
    }
    
    public String toString() {
        return "ServiceCall[className=" + className + ", methodHash="
                + methodHash + ", arguments=" + Arrays.asList(arguments)
                + ", context=" + context + "]";
    }
    
    public void readExternal(ObjectInput in) throws IOException,
            ClassNotFoundException {
        className = in.readUTF();
        methodHash = in.readInt();
        arguments = (Object[]) in.readObject();
        context = (Map) in.readObject();
    }
    
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(className);
        out.writeInt(methodHash);
        out.writeObject(arguments);
        out.writeObject(context);
    }
}
